package com.ruoyi.project.warehouseSystem.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.warehouseSystem.mapper.ComptClassifyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.warehouseSystem.mapper.ComponentMapper;
import com.ruoyi.project.warehouseSystem.domain.Component;
import com.ruoyi.project.warehouseSystem.service.IComponentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配件信息Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-22
 */
@Service
public class ComponentServiceImpl implements IComponentService 
{
    @Autowired
    private ComponentMapper componentMapper;
    @Autowired
    private ComptClassifyMapper comptClassifyMapper;

    /**
     * 查询配件信息
     * 
     * @param id 配件信息ID
     * @return 配件信息
     */
    @Override
    public Component selectComponentById(Long id)
    {
        return componentMapper.selectComponentById(id);
    }

    /**
     * 查询配件信息列表
     * 
     * @param component 配件信息
     * @return 配件信息
     */
    @Override
    public List<Component> selectComponentList(Component component)
    {
        if(component.getClassifyId()!=null){
            //获取所有分类子节点
            List<Long> pids = comptClassifyMapper.selectIdsByParentId(component.getClassifyId());
            if(pids.size()>0){
                component.setClassifyIds(pids);
            }
            component.setClassifyId(null);
        }
        return componentMapper.selectComponentList(component);
    }

    /**
     * 新增配件信息
     * 
     * @param component 配件信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertComponent(Component component)
    {
        component.setCreateBy(SecurityUtils.getUsername());
        component.setGmtCreate(new Date());
        int i = componentMapper.insertComponent(component);
        if(i>0){
            String str = "CMN" + String.format("%06d", component.getId());
            component.setCompSn(str);
            componentMapper.updateComponent(component);
        }
        return i;
    }

    /**
     * 修改配件信息
     * 
     * @param component 配件信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateComponent(Component component)
    {
        component.setModifyBy(SecurityUtils.getUsername());
        component.setGmtModify(new Date());
        return componentMapper.updateComponent(component);
    }

    /**
     * 批量删除配件信息
     * 
     * @param ids 需要删除的配件信息ID
     * @return 结果
     */
    @Override
    public int deleteComponentByIds(Long[] ids)
    {
        return componentMapper.deleteComponentByIds(ids);
    }

    /**
     * 删除配件信息信息
     * 
     * @param id 配件信息ID
     * @return 结果
     */
    @Override
    public int deleteComponentById(Long id)
    {
        return componentMapper.deleteComponentById(id);
    }
}
